package com.jackchen.common.i18n;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
import org.apache.log4j.Logger;

import com.jackchen.common.filter.LoginFilter;
import com.jackchen.service.UserService;

import javax.servlet.http.HttpSession;

public class I18nImpl implements I18n{
	
	private static final Logger log = Logger.getLogger(LoginFilter.class);
	
	//@Autowired
	private SessionLocaleResolver localeResolver;
	
	//@Autowired
	private UserService userService;

	public void setLocalLanguage(HttpServletRequest a_request,HttpServletResponse a_response){
		HttpSession httpSession = a_request.getSession();
		String userName =  (String)httpSession.getAttribute(LoginFilter.SESSION_USER_NAME);
		Integer languageFlag = (Integer) httpSession.getAttribute(LoginFilter.SESSION_USER_LANGUAGE);
		//if(null == userName){
		//	log.debug("user name is null ,you haven't loged in!");
		//	return;
		//}
		//User loginUser = userService.queryUserByUserName(userName);
		if(languageFlag==null||1 == (int)languageFlag){
			localeResolver.setLocale(a_request, a_response, Locale.SIMPLIFIED_CHINESE);
		}else{
			localeResolver.setLocale(a_request, a_response, Locale.US);
			log.debug("session set attribute:"+a_request.getSession().getAttribute(SessionLocaleResolver.class.getName() + ".LOCALE"));
		}
	}


	public SessionLocaleResolver getLocaleResolver() {
		return localeResolver;
	}


	public void setLocaleResolver(SessionLocaleResolver localeResolver) {
		this.localeResolver = localeResolver;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
}
